package net.aynt.core.util.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * 存储服务
 */
public abstract class QiNiuStorage {
	
	private final static SimpleDateFormat FMT_FN = new SimpleDateFormat("yyyy/MM/dd/H_mm_ss_");
	private final static String ACCESS_KEY = "zuq8V2T1AXEm9QKR_KqRSVYc_2TT7VffDCQtOV-2";
	private final static String SECRET_KEY = "s61fWHEQcq_gR_Fwt0KLTfjRkFb5s6r2nk_x8AOl";
	Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	
	public abstract String getBasePath();

	/**
	 * 读取文件数据
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 */
	public FileInputStream read(String path) throws FileNotFoundException {
		return new FileInputStream(getBasePath() + path);
	}

	/**
	 * 读取文件
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 */
	public File readFile(String path) throws FileNotFoundException {
		return new File(getBasePath() + path);
	}
	
	/**
	 * 判断文件是否存在
	 * @param path
	 * @return
	 */
	public boolean exist(String path) {
		return new File(getBasePath() + path).exists();
	}
	
	
	/**
	 * 保存文件，并返回对应的文件URI
	 * @param f
	 * @return
	 * @throws IOException 
	 */
	public String save(InputStream ins, String ext) {
		String path = FMT_FN.format(new Date()) + 
				RandomStringUtils.randomAlphanumeric(4) + '.' + ext;
		byte[] buffer = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int n;
		try {
			while ((n = ins.read(b)) != -1)
			{
				bos.write(b, 0, n);
			}
			ins.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			Response res =   uploadManager.put(buffer, getBasePath() + path, getUpToken());
		} catch (QiniuException e) {
			e.printStackTrace();
		}
		return path;
	}
	
	
	private String getUpToken(){
	    return auth.uploadToken("ayntnet");
	}
	private UploadManager uploadManager = new UploadManager();
	/**
	 * 保存文件，并返回对应的文件URI
	 * @param f
	 * @return
	 * @throws IOException 
	 */
	public String save(File f) throws IOException {    
		String uri = FMT_FN.format(new Date()) + 
			RandomStringUtils.randomAlphanumeric(4) + '.' + 
			FilenameUtils.getExtension(f.getName()).toLowerCase();
		return save(f, uri);
	}

	/**
	 * 保存文件到指定路径
	 * @param f
	 * @param path
	 * @throws QiniuException 
	 * @throws IOException 
	 */
	public String save(File f, String path){		
		try {
			Response res =   uploadManager.put(f, getBasePath() + path, getUpToken());
		} catch (QiniuException e) {
			e.printStackTrace();
		}
		return path;
	}
	
	/**
	 * 根据路径来删除文件
	 * @param path
	 * @throws IOException
	 */
	public void delete(String path) throws IOException {
		File dest = new File(getBasePath() + path);
		FileUtils.forceDelete(dest);
	}
	
}
